package com.icss.th.db.pojo;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Column 
{
	/**
	 * 列名
	 */
	private String name;
	
	/**
	 * 列类型
	 */
	private int type;
	
	public String getName() 
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}

	public int getType() 
	{
		return type;
	}

	public void setType(int type) 
	{
		this.type = type;
	}

	public Column(String name, int type)
	{
		this.name = name;
		this.type = type;
	}
	
	/**
	 * 根据本列信息读取字段值
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	public Object getValue(ResultSet rs) throws SQLException 
	{
		if(type == java.sql.Types.INTEGER)
		{
			return rs.getInt(name);
		}
		else if (type == java.sql.Types.DATE)
		{
			return rs.getDate(name);
		}
		else
		{
			return rs.getString(name);
		}
	}
	
	/**
	 * 根据本列信息插入值
	 * @param ps
	 * @param value
	 * @param index
	 * @throws SQLException
	 */
	public void setValue(PreparedStatement ps, Object value, int index) throws SQLException 
	{
		if(type == java.sql.Types.INTEGER)
		{
			ps.setInt(index, value == null? null : (Integer)value);
		}
		else if (type == java.sql.Types.DATE)
		{
			ps.setDate(index, value == null? null : (Date)value);
		}
		else
		{
			ps.setString(index, value == null? null : (String)value);
		}
	}
}
